package writeToExcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import constants.Constants;

public class WritePercentageToExcel {

	SXSSFWorkbook wb = new SXSSFWorkbook(-1);

	public WritePercentageToExcel(String dataset, String fold,
			double[][] posPercent, double[][] neuPercent, double[][] negPercent)
			throws IOException, InvalidFormatException {

		String[] epsilons = { "0", "05", "1", "15", "2", "25" };
		String excel_dir = Constants.excel_dir;
		String excel_file = excel_dir + dataset + "-fold-" + fold
				+ "-Percentage.xlsx";

		Sheet sh = wb.getSheet("Percentage");
		if (sh == null) {
			sh = wb.createSheet("Percentage");
		}

		Row row0 = sh.createRow(0);
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 5; j++) {
				Cell cell = row0.createCell(1 + 5 * i + j);
				cell.setCellValue("e=0." + epsilons[i] + ", m="
						+ (0.0 + 0.5 * j));
			}
		}

		createNewSheet(sh, "Positive Percentage", epsilons, posPercent, 0);
		createNewSheet(sh, "Neutral Percentage", epsilons, neuPercent, 1);
		createNewSheet(sh, "Negative Percentage", epsilons, negPercent, 2);

		FileOutputStream file = new FileOutputStream(new File(excel_file));
		wb.write(file);
	}

	private void createNewSheet(Sheet sh, String sheetName, String[] epsilons,
			double[][] data, int x) {
		Row row = sh.getRow(1 + x);
		if (row == null) {
			row = sh.createRow(1 + x);
		}
		Cell cell0 = row.createCell(0);
		cell0.setCellValue(sheetName);
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 5; j++) {
				Cell cell = row.createCell(1 + 5 * i + j);
				cell.setCellValue(Double.parseDouble(String.format("%.1f",
						data[i][j])));
			}
		}
	}

}
